﻿//-----------------------------------------------------------------------
// <copyright file="BusinessRegexRule.cs" company="FastNET Services">
//     Copyright (c) 2007-2013. All Rights Reserved.
// </copyright>
// <warranty>
//     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
//     BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </warranty>
//-----------------------------------------------------------------------
#region Usings

using System;
using System.Text.RegularExpressions;

#endregion

namespace FastNet.BusinessSystem.Rules
{
    /// <summary>
    /// Class used for managing business rules for an object
    /// </summary>
    public sealed class BusinessRegexRule : BusinessRule
    {
        #region Variables

        private readonly Func<string> _validateMethod;
        private readonly Regex _regex;

        #endregion

        #region CTor/DTor

        /// <summary>
        /// Initializes a new instance of the BusinessRegexRule class
        /// </summary>
        /// <param name="ruleName">Name of the rule</param>
        /// <param name="propertyName">Name of the property the rule validates</param>
        /// <param name="errorMessage">Message to display when in error</param>
        /// <param name="validateMethod">Method/Lambda to call when validating rule</param>
        /// <param name="regex">Regular expression string to compare against</param>
        public BusinessRegexRule(string ruleName, string propertyName, string errorMessage, Func<string> validateMethod, string regex)
            : this(ruleName, propertyName, errorMessage, validateMethod, regex, RegexOptions.None)
        {
        }

        /// <summary>
        /// Initializes a new instance of the BusinessLambdaRule class
        /// </summary>
        /// <param name="ruleName">Name of the rule</param>
        /// <param name="propertyName">Name of the property the rule validates</param>
        /// <param name="errorMessage">Message to display when in error</param>
        /// <param name="validateMethod">Method/Lambda to call when validating rule</param>
        /// <param name="pattern">Regular expression string to compare against</param>
        /// <param name="options">Regular expression options to use when comparing</param>
        public BusinessRegexRule(string ruleName, string propertyName, string errorMessage, Func<string> validateMethod, string pattern, RegexOptions options)
            : base(ruleName, propertyName, errorMessage)
        {
            _validateMethod = validateMethod;
            _regex = new Regex(pattern, options);
            Pattern = pattern;
            Options = options;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets the pattern used during matching
        /// </summary>
        public string Pattern { get; private set; }

        /// <summary>
        /// Gets the regular expression options used during matching
        /// </summary>
        public RegexOptions Options { get; private set; }

        #endregion

        #region Methods

        /// <summary>
        /// Called to check the validity of the rule
        /// </summary>
        /// <returns>Boolean whether or not the rule is valid</returns>
        public override bool IsValid() { return _regex.IsMatch(_validateMethod()); }

        #endregion
    }
}
